Skip to content

Move Zeroes ​

Move Zeroes — LeetCode

Move all the 0s in an array to the end while keeping the relative order of the non-zero elements, doing it in place.

Approach ​

Two pointers: l will iterate over elements one by one, r will find the next non-zero number. if a[l] = 0 and a[r] != 0, and r > l, swap. else move the r once. All elements between l and r will be zeroes. Iterate till one pointer reaches the end.

Remarks ​